//
//  LSMarqueeView.m
//  LSMarqueeView
//
//  Created by WangBiao on 2017/7/19.
//  Copyright © 2017年 LSRain. All rights reserved.
//

#import "LSMarqueeView.h"

#define M 0.f
@interface LSMarqueeView ()

@property (nonatomic, assign) NSInteger seconds;
@property (nonatomic, strong) UIView *backView;

@end

@implementation LSMarqueeView


#pragma mark - Action

-(void)labelClick:(UITapGestureRecognizer *)tap {
    if (self.clickBlock) {
        self.clickBlock((UIView *)[tap view]);
    }
}

#pragma mark - Lasy load

- (NSMutableArray *)lsViewArr{
    if (!_lsViewArr) {
        _lsViewArr = @[].mutableCopy;
    }
    return _lsViewArr;
}

- (UIView *)backView{
    if (!_backView) {
        _backView = [[UIView alloc] initWithFrame:self.bounds];
        _backView.backgroundColor = [UIColor whiteColor];
        _backView.clipsToBounds = YES;
        _backView.userInteractionEnabled = YES;
        [self addSubview:_backView];
    }
    return _backView;
}

#pragma mark - init Data

- (void)initLabels{
    [self.lsViewArr enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self.backView addSubview:obj];
        obj.hidden = YES;
        obj.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick:)];
        [obj addGestureRecognizer:tap];
    }];
}

#pragma mark - 定时器start

- (void)startCountdown {
    // GCD Timer
    WeakSelf
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 3.75 * NSEC_PER_SEC, 0); // 每6S
    dispatch_source_set_event_handler(_timer, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf timerRepeat];
            _seconds++;
        });
    });
    dispatch_resume(_timer);
}

#pragma mark - 定时执行方法

- (void)timerRepeat {
    WeakSelf
    UIView *targetView = [self.lsViewArr objectAtIndex:_seconds % self.lsViewArr.count];
    targetView.hidden = NO;
    if (_seconds <= self.lsViewArr.count) {
        targetView.frame = CGRectMake(M, self.backView.frame.size.height, self.backView.frame.size.width - 2 * M, self.backView.frame.size.height);
    }
    [UIView animateWithDuration:.75f animations:^{
        targetView.frame = CGRectMake(M, weakSelf.backView.frame.origin.y, weakSelf.backView.frame.size.width - 2 * M, weakSelf.backView.frame.size.height);
    } completion:^(BOOL finished) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:.75f animations:^{
                targetView.frame = CGRectMake(M, -weakSelf.backView.frame.size.height, weakSelf.backView.frame.size.width - 2 * M, weakSelf.backView.frame.size.height);
            } completion:^(BOOL finished) {
                targetView.frame = CGRectMake(M, weakSelf.backView.frame.size.height, weakSelf.backView.frame.size.width - 2 * M, weakSelf.backView.frame.size.height);
            }];
        });
    }];
}

#pragma mark - 自定义构造函数

- (instancetype)initWithFrame:(CGRect)frame andViewArr:(NSMutableArray *)views
{
    self = [super initWithFrame:frame];
    if (self) {
        self.lsViewArr = views;
        [self initLabels];
    }
    return self;
}

@end
